/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package da523b;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.FlowLayout;

/**
 *
 * @author linuslofberg
 */
public class GUI extends javax.swing.JFrame {

    public GUI() {
        initComponents();
        this.setBackground(Color.white);
    }
    private Sql sql;

    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;

    private JTextField textFieldID;
    private JTextField textFieldFirstName;
    private JTextField textFieldLastName;
    private JTextField textFieldPhoneNumber;

    private JLabel labelID;
    private JLabel labelFirstName;
    private JLabel labelLastName;
    private JLabel labelPhoneNumber;

    private JButton searchButton;
    private JButton addButton;
    private JButton updateButton;
    private JButton listButton;

    private JScrollPane scrollpane;
    private JTable table;

    private void initComponents() {
        sql = new Sql();

        panel1 = new JPanel();

        searchButton = new JButton("SÖK!");
        updateButton = new JButton("UPPDATERA!");
        addButton = new JButton("LÄGG TILL!");
        listButton = new JButton("LIST!");

        labelID = new JLabel("ID");
        labelFirstName = new JLabel("Förnamn");
        labelLastName = new JLabel("Efternamn");
        labelPhoneNumber = new JLabel("Telefon");

        textFieldID = new JTextField();
        textFieldFirstName = new JTextField();
        textFieldLastName = new JTextField();
        textFieldPhoneNumber = new JTextField();

        table = new JTable(15, 4);
        scrollpane = new JScrollPane(table);

        this.setSize(600, 530);
        this.setResizable(false);
        this.setLocation(150, 200);
        this.add(panel1);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        textFieldID.setColumns(10);
        textFieldFirstName.setColumns(10);
        textFieldLastName.setColumns(10);
        textFieldPhoneNumber.setColumns(10);

        panel1.add(labelID);
        panel1.add(textFieldID);
        panel1.add(labelFirstName);
        panel1.add(textFieldFirstName);
        panel1.add(labelLastName);
        panel1.add(textFieldLastName);
        panel1.add(labelPhoneNumber);
        panel1.add(textFieldPhoneNumber);
        panel1.add(searchButton);
        panel1.add(updateButton);
        panel1.add(addButton);
        panel1.add(listButton);
        panel1.add(scrollpane);

        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ResultSet rs;
                try {
                    if (!textFieldID.getText().isEmpty()) {
                        rs = sql.search("SELECT * FROM phonebook WHERE id=" + textFieldID.getText());
                    } else {
                        rs = sql.search("SELECT * FROM `phonebook` WHERE `fornamn` LIKE '" + textFieldFirstName.getText() + "' OR `efternamn` LIKE '" + textFieldLastName.getText() + "'OR `telefon` LIKE '" + textFieldPhoneNumber.getText() + "' LIMIT 0 , 30");
                    }
                    int row = 0;
                    while (rs.next()) {
                        table.setValueAt(rs.getInt("id"), row, 0);
                        table.setValueAt(rs.getString("fornamn"), row, 1);
                        table.setValueAt(rs.getString("efternamn"), row, 2);
                        table.setValueAt(rs.getString("telefon"), row, 3);
                        row++;
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //ResultSet rs;
                try {
                    if (!textFieldID.getText().isEmpty()) {
                        sql.update("UPDATE `phonebook` SET `fornamn` = '" + textFieldFirstName.getText() + "',`efternamn` = '" + textFieldLastName.getText() + "',`telefon` = '" + textFieldPhoneNumber.getText() + "' WHERE `id` =" + textFieldID.getText());
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });

        listButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ResultSet rs;
                try {
                    rs = sql.search("SELECT * FROM phonebook");
                    int row = 0;
                    while (rs.next()) {
                        table.setValueAt(rs.getInt("id"), row, 0);
                        table.setValueAt(rs.getString("fornamn"), row, 1);
                        table.setValueAt(rs.getString("efternamn"), row, 2);
                        table.setValueAt(rs.getString("telefon"), row, 3);
                        row++;
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });

        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.print("argag");
                if (!textFieldFirstName.getText().isEmpty() && !textFieldLastName.getText().isEmpty() && !textFieldPhoneNumber.getText().isEmpty()) {
                    try {
                        sql.insert(textFieldFirstName.getText(), textFieldLastName.getText(), textFieldPhoneNumber.getText());
                    } catch (SQLException ex) {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }
}
